package com.cdck.safe.dete.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.cdck.safe.dete.mapper.ResourceRecordMapper;
import com.cdck.safe.dete.model.entity.BookRecord;
import com.cdck.safe.dete.model.entity.Booking;
import com.cdck.safe.dete.model.entity.Resource;
import com.cdck.safe.dete.model.entity.ResourceRecord;
import com.cdck.safe.dete.model.vo.ResourceStatisticVO;
import com.cdck.safe.dete.service.*;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.awt.print.Book;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
public class DataStatisticsServiceImpl implements DataStatisticsService {
    private final BookingService bookingService;

    private  final ResourceRecordService resourceRecordService;

    private final ResourceRecordMapper resourceRecordMapper;

    private final ResourceService resourceService;

    @Override
    public Map getRecentBookingData(Integer dayLength) {
        Map<String, Object> map = new HashMap<>();
        // 获取最近天的日历
        LocalDateTime dateTime = LocalDateTime.now();
        List<String> days = new ArrayList<>();
        for (int i = dayLength - 1; i >= 0; i--) {
            LocalDateTime localDateTime = dateTime.plusDays(-i);
            days.add(localDateTime.format(DateTimeFormatter.ofPattern("MM月dd日")));
        }
        map.put("dataTime", days);
        List<Integer> data = new ArrayList<>();
        for (int i = dayLength - 1; i >= 0; i--) {
            LocalDateTime localDateTime = dateTime.plusDays(-i);
            LocalDateTime minTime = localDateTime.with(LocalTime.MIN);
            LocalDateTime maxTime = localDateTime.with(LocalTime.MAX);
            long count = bookingService.count(new LambdaQueryWrapper<Booking>()
                    .between(Booking::getCreateTime, minTime, maxTime)
            ) + resourceRecordService.count(new LambdaQueryWrapper<ResourceRecord>()
                    .between(ResourceRecord::getCreateTime, minTime, maxTime)
            );
            data.add((int) count);
        }
        map.put("lineData", data);
        return map;
    }

    @Override
    public Map getTopBookingData() {
       Map<String, Object> map = new HashMap<>();
        // 总预约数
        long total = bookingService.count() + resourceRecordService.count();
        map.put("total",total);
        // 今日预约数
        LocalDateTime dateTime = LocalDateTime.now();
        LocalDateTime minTime = dateTime.with(LocalTime.MIN);
        LocalDateTime maxTime = dateTime.with(LocalTime.MAX);
        long todayNum = bookingService.count(new LambdaQueryWrapper<Booking>()
                .between(Booking::getCreateTime, minTime, maxTime)
        ) + resourceRecordService.count(new LambdaQueryWrapper<ResourceRecord>()
                .between(ResourceRecord::getCreateTime, minTime, maxTime));
        map.put("todayNum",todayNum);

        // 今日已通过
        long todayPassCheck = bookingService.count(new LambdaQueryWrapper<Booking>()
                .between(Booking::getCreateTime, minTime, maxTime)
                .eq(Booking::getStatus, 2)) + resourceRecordService.count(new LambdaQueryWrapper<ResourceRecord>()
                .between(ResourceRecord::getCreateTime, minTime, maxTime)
                .eq(ResourceRecord::getStatus, 2)
                .or().eq(ResourceRecord::getStatus, 3)
        );
        map.put("todayPassCheck",todayPassCheck);
        // 今日待审核
        long todayAwaitCheck = bookingService.count(new LambdaQueryWrapper<Booking>()
                .between(Booking::getCreateTime, minTime, maxTime)
                .eq(Booking::getStatus, 0)
        ) + resourceRecordService.count(new LambdaQueryWrapper<ResourceRecord>()
                .between(ResourceRecord::getCreateTime, minTime, maxTime)
                .eq(ResourceRecord::getStatus, 0)
        );
        map.put("todayAwaitCheck",todayAwaitCheck);
        return map;
    }

    @Override
    public List<ResourceStatisticVO> getRecentResource() {
        List<ResourceStatisticVO> result = resourceRecordMapper.getResourceUseData();
        return result;
    }

    @Override
    public Map getResourceUse() {
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime startTime = now.minusDays(30);
        List<ResourceRecord> resourceRecords = resourceRecordService.list(
                new LambdaQueryWrapper<ResourceRecord>()
                        .eq(ResourceRecord::getStatus, 3)
                        .between(ResourceRecord::getCreateTime, startTime, now
                        )
        );

        Map<Long, String> resourceMap = resourceService.list().stream().collect(Collectors.toMap(Resource::getId, Resource::getName));

        // key 为resourceId
        Map<Long, List<ResourceRecord>> listMap = resourceRecords.stream().collect(Collectors.groupingBy(ResourceRecord::getResourceId));

        HashMap<String, Object> HashMap = new HashMap<>();
        List<String> xAxisData = new ArrayList<>();
        List<Integer> seriesData = new ArrayList<>();
        for (Long reId : listMap.keySet()) {
            int size = listMap.get(reId).size();
            xAxisData.add(resourceMap.get(reId));
            seriesData.add(size);
        }

        HashMap.put("xAxisData", xAxisData);
        HashMap.put("seriesData", seriesData);
        return HashMap;
    }

}
